package wasnake.actors;

import java.awt.Color;
import java.util.HashSet;
import java.util.Set;
import wasnake.grid.Grid;
import wasnake.grid.Location;

/**
 * A single cell actor which waits to be eaten by another actor.
 * @author Mark
 */
public class Food extends Actor {
    /** The cell that composes this food.*/
    private Cell cell;
    
    /**
     * Creates a new food item.
     * @param color The color of the food.
     */
    public Food(Color color) {
        super(color);
    }

    @Override
    public void putSelfInGrid(Grid<Cell> grid, Location location) {
    	synchronized(grid) {
	        if(this.grid != null)
	            throw new IllegalStateException(
	                    "This food is already contained in a grid.");
	        if(!grid.isValid(location))
	            throw new IllegalStateException(
	                    "The location given is not valid.");
	        this.grid = grid;
	        if(grid.get(location) != null)
	            grid.get(location).getOwner().removeSelfFromGrid();
	        cell = new Cell(this, location);
	        grid.put(location, cell);
    	}
    }

    @Override
    public void removeSelfFromGrid() {
    	synchronized(grid) {
	        if(cell == null)
	            throw new IllegalStateException(
	                    "This food is not contained in a grid.");
	        grid.remove(cell.getLocation());
	        cell = null;
	        grid = null;
    	}
    }

    @Override
    public Set<Cell> getCells() {
        Set<Cell> cells = new HashSet<Cell>();
        cells.add(cell);
        return cells;
    }

    @Override
    public void act() {
        //Do nothing, wait to be eaten
    }
    
}
